package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * 当前库存查询
     *
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {

        PageHelper.startPage(page, rows);

        Page<Goods> pageGoods = goodsDao.selectPage(codeOrName);

        for (Goods goods : pageGoods) {
            Integer saleNum = goodsDao.getSaleTotalByGoodsId(goods.getGoodsId());
            if (saleNum != null) {
                goods.setSaleTotal(saleNum);
            } else {
                goods.setSaleTotal(0);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageGoods.getTotal());
        map.put("rows", pageGoods);


        return map;
    }


    /**
     * 获取分类
     * <p>
     * //     * @param goodsTypeId
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> classify() {
        GoodsType goodsType = goodsDao.getAllGoodsType();
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(this.map(goodsType));

        return list;
    }

    private Map<String, Object> map(GoodsType goodsType) {
//        List<GoodsType> allGoodsType = goodsDao.getAllGoodsType();
        Map<String, Object> map = new HashMap<>();
//        for (GoodsType goodsType : allGoodsType) {

        map.put("text", goodsType.getGoodsTypeName());
        map.put("id", goodsType.getGoodsTypeId());
        if (goodsType.getGoodsTypeState() == 0) {

            map.put("state", "open");
        } else {
            map.put("state", "closed");
        }
        map.put("iconCls", "goods-type");
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("state", goodsType.getGoodsTypeState());
        map.put("attributes", hashMap);

        Integer id = goodsType.getGoodsTypeId();
        List<GoodsType> goodsTypeList = goodsDao.selectGoodsType(id);
        List<Map<String, Object>> list = new ArrayList<>();
        if (goodsTypeList != null) {
            for (GoodsType type : goodsTypeList) {
                list.add(map(type));
            }
            map.put("children", list);
        }
        return map;
    }

    /**
     * 添加
     *
     * @param goodsTypeName
     * @param pId
     * @return
     */
    @Override
    public ServiceVO save(String goodsTypeName, Integer pId) {
        Integer goodsTypeState = 0;
        goodsDao.save(goodsTypeName, pId, goodsTypeState);
        List<GoodsType> all = goodsDao.selectAll();
        for (GoodsType type : all) {
            if (type.getGoodsTypeId() == pId) {
                type.setGoodsTypeState(1);
                goodsDao.updateType(type);
            }
        }
        return new ServiceVO(100, "添加成功");
    }

    /**
     * 删除
     *
     * @param goodsTypeId
     */
    @Override
    public void delete(Integer goodsTypeId) {
        GoodsType goodsType = goodsDao.selectType(goodsTypeId);
        Integer pId = goodsType.getPId();
        List<GoodsType> goodsTypes = goodsDao.selectGoodsType(pId);
        if (goodsTypes.size() == 1) {
            GoodsType type = goodsDao.selectType(pId);
            type.setGoodsTypeState(0);
            goodsDao.updateType(type);
        }
        goodsDao.delete(goodsTypeId);


    }

    /**
     * 删除goods
     *
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        Goods goods = goodsDao.selectGoods(goodsId);
        if (goods.getState().equals(0)) {
            goodsDao.deleteGoods(goodsId);
        }
        return new ServiceVO(100, "请求成功");
    }

    /**
     * 添加或修改
     *
     * @param goods
     * @return
     */
    @Override
    public ServiceVO saveOrUpdateGoods(Goods goods) {
        Integer goodsId = goods.getGoodsId();
        Goods goodsOne = goodsDao.selectGoods(goodsId);
        if (null == goodsOne) {
            //添加
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsDao.saveGoods(goods);
        } else {
            goodsDao.updateGoods(goods);
            //修改
        }
        return new ServiceVO(100, "请求成功");
    }

    /**
     * 分页查询无库存商品信息
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page, rows);
        Page<Goods> pageGoodsList = goodsDao.getNoInventoryQuantity(nameOrCode);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageGoodsList.getTotal());
        map.put("rows", pageGoodsList);
        return map;
    }


    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {

        PageHelper.startPage(page, rows);
        Page<Goods> HasInventoryQuantity = goodsDao.getHasInventoryQuantity(nameOrCode);
        Map<String, Object> map = new HashMap<>();
        map.put("total", HasInventoryQuantity.getTotal());
        map.put("rows", HasInventoryQuantity);
        return map;
    }

    /**
     * 添加库存、修改数量或成本价
     *
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = goodsDao.selectGoods(goodsId);
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goodsDao.updateGoods(goods);
        return new ServiceVO(100, "修改成功");
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     *
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsDao.selectGoods(goodsId);
        if (goods.getState().equals(0)) {
            goods.setInventoryQuantity(0);
            goodsDao.updateGoods(goods);
        }
        return new ServiceVO(100, "删除库存成功");
    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     *
     * @return
     */
    @Override
    public Map<String, Object> listAlarm() {
        Page<Goods> listAlarm = goodsDao.listAlarm();
        Map<String, Object> map = new HashMap<>();
        map.put("rows", listAlarm);
        return map;
    }
}
